import Vue from 'vue'
import App from './App'


Vue.config.productionTip = false
const loginByWeixin = function() {
	this.getWeixinCode().then((code) => {
		return uniCloud.callFunction({
			name: 'uni-id-test',
			data: {
				action: 'loginByWeixin',
				params: {
					code,
				}
			}
		})
	}).then((res) => {
		console.log(res.result)
		uni.showModal({
			showCancel: false,
			content: res.result.msg
		})
		if (res.result.code === 0) {
			uni.setStorageSync('openid', res.result.openid)
			// uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)
		}
	}).catch((e) => {
		console.error(e)
		uni.showModal({
			showCancel: false,
			content: '微信登录失败，请稍后再试'
		})
	})
}
const getWeixinCode = function() {
	return new Promise((resolve, reject) => {
		// #ifdef APP-PLUS
		weixinAuthService.authorize(function(res) {
			resolve(res.code)
		}, function(err) {
			console.log(err)
			reject(new Error('微信登录失败'))
		});
		// #endif
		// #ifdef MP-WEIXIN
		uni.login({
			provider: 'weixin',
			success(res) {
				resolve(res.code)
			},
			fail(err) {
				reject(new Error('微信登录失败'))
			}
		})
		// #endif
	})
}
const getUserProfile = function() {
	let _this = this
	uni.getUserProfile({
		desc: "显示用户头像和用户名",
		lang: "zh_CN",
		// provider: "weixin",
		success: res => {
			// const userWeiXinAccInfo = res.userInfo;
			console.log(res);
			uni.setStorageSync('avatarUrl',res.userInfo.avatarUrl)
			uni.setStorageSync('nick_name',res.userInfo.nickName)
			 _this.loginByWeixin()
		},
		fail: err => {
			console.log(err);
		}
	})
}
Vue.prototype.$uniLogin = {
	loginByWeixin,
	getWeixinCode,
	getUserProfile
}

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
